package com.atguigu.flink.watermark;

import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/11/23
 *
 *      需要先调用产生水印的算子，从数据中提取某个字段(时间相关)作为水印
 *
 *      结论：  ①水印永远单调递增，水印就是时间，时间不会倒流
 *             ②timerService.currentWatermark();
 *                  这个方法没意义，打印的是这条数据更新前的水印。落后数据一个身位
 *             ③水印是周期型发送
 */
public class Demo1_WaterMark
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //设置水印周期发送的间隔
        env.getConfig().setAutoWatermarkInterval(2000);

        //初次完，先设置为1
        env.setParallelism(1);

        //水印策略
        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            .<WaterSensor>forMonotonousTimestamps()   //目前的场景数据都是有序的
                                                      //如何产生水印
                                                      .withTimestampAssigner((e, r) -> e.getTs());

        env
           .socketTextStream("hadoop103", 8888)
           .map(new MapFunction<String, WaterSensor>()
           {
               @Override
               public WaterSensor map(String value) throws Exception {
                   String[] data = value.split(",");
                   return new WaterSensor(
                       data[0],
                       Long.valueOf(data[1]),
                       Integer.valueOf(data[2])
                   );
               }
           })
           //负责基于水印策略生成水印
           .assignTimestampsAndWatermarks(watermarkStrategy)
           .process(new ProcessFunction<WaterSensor, String>()
           {
               @Override
               public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                   //获取和时间相关的属性
                   TimerService timerService = ctx.timerService();

                   //指当前收到这条数据时的 process算子时钟(水印)，参数上一条数据更新后的水印
                   // 或当前水印更新前的水印
                   long currentWatermark = timerService.currentWatermark();
                   long processingTime = System.currentTimeMillis();

                   out.collect("数据是:"+value +"----水印(EventTime):"+currentWatermark +"----物理时间(ProcessingTime):" + processingTime);
               }
           })
           .print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }
}
